#include "Jingle.h"
#include "stanza.h"
#include "JingleExtension.h"

namespace cm_nat
{
    using namespace cm_xmpp;

    Jingle::Jingle(ClientBase* parent, JingleListener* listener)
        : m_parent(parent),m_listener(listener)
    {
        if( !m_parent )
            return;

        m_parent->registerIqHandler( this, ExtJingle );
        m_parent->registerStanzaExtension( new JingleExtension() );
    }

    Jingle::~Jingle()
    {
        if( !m_parent )
            return;

        m_parent->removeIqHandler( this, ExtJingle);
        m_parent->removeIDHandler( this );
        m_parent->removeStanzaExtension( ExtJingle);
    }

    bool Jingle::handleIq(const IQ& iq )
    {
        const JingleExtension* p_je = iq.findExtension<JingleExtension>(ExtJingle);
        if( !p_je ){
            return false;
        }

        IQ re( IQ::Result, iq.from(), iq.id() );
        m_parent->send( re );

        if(m_listener){
            m_listener->onJingleTag(p_je->m_jingleTag);
        }

        return true;
    }

    void Jingle::handleIqID(const IQ& iq, int context )
    {

    }

}